package com.dayatang.hrm.compensation.domain;

import java.util.Date;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import com.dayatang.commons.domain.InstanceFactory;

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue("5")
public class SalaryDataSourcePropertySetting extends SalaryParameterSetting {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2846160155930857537L;

	private static SalaryDataSourcePropertySettingRepository salaryDataSourcePropertySettingRepository;

	@ManyToOne
	@JoinColumn(name = "datasource_property_id")
	private SalaryDataSourceProperty dataSourceProperty;

	public SalaryDataSourcePropertySetting() {
		// 只能为：直接取值
		// setDataCalculatePolicy(DirectlyCalculatePolicy.INSTANCE);
		// 范围设置和会计期间无关
		// setFiscalPeriodRangeSetting(null);
	}

	public SalaryDataSourcePropertySetting(
			SalaryDataSourceProperty dataSourceProperty) {
		this.dataSourceProperty = dataSourceProperty;
	}

	public static SalaryDataSourcePropertySettingRepository getSalaryDataSourcePropertySettingRepository() {
		if (salaryDataSourcePropertySettingRepository == null) {
			salaryDataSourcePropertySettingRepository = InstanceFactory
					.getInstance(SalaryDataSourcePropertySettingRepository.class);
		}
		return salaryDataSourcePropertySettingRepository;
	}

	public static void setSalaryDataSourcePropertySettingRepository(
			SalaryDataSourcePropertySettingRepository salaryDataSourcePropertySettingRepository) {
		SalaryDataSourcePropertySetting.salaryDataSourcePropertySettingRepository = salaryDataSourcePropertySettingRepository;
	}

	public SalaryDataSourceProperty getDataSourceProperty() {
		return dataSourceProperty;
	}

	public void setDataSourceProperty(
			SalaryDataSourceProperty dataSourceProperty) {
		this.dataSourceProperty = dataSourceProperty;
	}

	/*
	 * =======================================
	 * 
	 * action
	 * 
	 * ========================================
	 */

	public boolean isNameAlreadyUsedByOtherInSameSalaryAccountSuite(String name, Date fromDate,
			Date toDate) {
		if (isNew()) {
			return getSalaryDataSourcePropertySettingRepository()
					.isNameAlreadyUsedExceptSelf(name, fromDate, toDate, null);
		} else {
			return getSalaryDataSourcePropertySettingRepository()
					.isNameAlreadyUsedExceptSelf(name, fromDate, toDate, this);
		}
	}

	public static SalaryDataSourcePropertySetting init(
			SalaryDataSourceProperty dataSourceProperty) {
		return new SalaryDataSourcePropertySetting(dataSourceProperty);
	}

	public static SalaryDataSourcePropertySetting get(
			SalaryDataSourceProperty dataSourceProperty, Date queryDate) {
		return getSalaryDataSourcePropertySettingRepository().get(
				dataSourceProperty, queryDate);
	}
	
	@Override
	public SalaryParameterSetting getNewInstance(SalaryParameter salaryParameter) {
		return new SalaryDataSourcePropertySetting(
				(SalaryDataSourceProperty) salaryParameter);
	}

}
